package com.rfid.afd.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;

import com.rfid.afd.activity.adapter.ShangjiaAdapter;
import com.rfid.afd.activity.adapter.SyncAdapter;
import com.rfid.afd.activity.view.ISyncView;
import com.rfid.afd.presenter.SyncPresenter;
import com.rfid.rls_psw.App;
import com.rfid.rls_psw.R;
import com.rfid.util.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import butterknife.BindView;
import butterknife.ButterKnife;
/**同步，用于上传操作记录与剔除信息，也下载最新的数据库文件*/
public class SyncActivity extends BaseActivity<SyncPresenter> implements ISyncView, Observer {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.quit)
    Button mQuit;
    @BindView(R.id.upload)
    Button mUpload;
    @BindView(R.id.download)
    Button mDownload;

    SyncAdapter mAdapter;
    List<String> mInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sync);
        ButterKnife.bind(this);
        mInfo = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        mAdapter = new SyncAdapter(this,mInfo);
        mRecyclerView.setAdapter(mAdapter);
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
        App.getInstance().getObservable().addObserver(this);
    }

    @Override
    public SyncPresenter getPresenter() {
        return new SyncPresenter();
    }

    @Override
    public void onOperateFileBegain() {
        System.out.println("开始生成操作记录文件");
        mInfo.add(0,Util.getDateTime() + "    开始生成操作记录文件");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onOperateFileMaked(int count) {
        if(count > 0 ) {
            mInfo.add(0,Util.getDateTime() + "   " + count + "条操作记录文件生成成功");
        } else {
            mInfo.add(0,Util.getDateTime() + "   " + "无操作记录");
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onOperateFileMakeFail() {
        mInfo.add(0,Util.getDateTime() +  "    操作记录文件生成失败");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onUploadFileStart(String name) {
        mInfo.add(0,Util.getDateTime() +  "   开始上传文件" + name);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onUploadFileSuccess() {
        mInfo.add(0,Util.getDateTime() +  "    文件上传成功");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onUploadFileFail() {
        mInfo.add(0,Util.getDateTime() +  "   文件上传失败");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onUploadOver() {
        mInfo.add(0,Util.getDateTime() +  "    文件上传结束");
        mAdapter.notifyDataSetChanged();
        mQuit.setEnabled(true);
        mUpload.setEnabled(true);
        mDownload.setEnabled(true);
    }

    @Override
    public void onShunjiaFileBegain() {
        mInfo.add(0,Util.getDateTime() +  "   开始生成顺架记录文件");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onShunjiaFileCreated(int count) {
        if(count > 0 ) {
            mInfo.add(0,Util.getDateTime() + "   " + count + "条顺架记录文件生成成功");
        } else {
            mInfo.add(0,Util.getDateTime() + "   " + "无顺架记录");
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onShunjiaFileFail() {
        mInfo.add(0,Util.getDateTime() +  "   顺架文件生成失败");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDownloadBegain() {
        mInfo.add(0,Util.getDateTime() +  "   开始下载服务端数据");
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDownloadSuccess() {
        mInfo.add(0,Util.getDateTime() +  "   服务端数据下载成功");
        mAdapter.notifyDataSetChanged();
        mQuit.setEnabled(true);
        mUpload.setEnabled(true);
        mDownload.setEnabled(true);
    }

    @Override
    public void onDownloadFail() {
        mInfo.add(0,Util.getDateTime() +  "   服务端数据下载失败");
        mAdapter.notifyDataSetChanged();
        mQuit.setEnabled(true);
        mUpload.setEnabled(true);
        mDownload.setEnabled(true);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void stopLoading() {
    }

    public void upload(View view) {
        mQuit.setEnabled(false);
        mUpload.setEnabled(false);
        mDownload.setEnabled(false);
        mPresenter.upload();
    }

    public void quit(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        if(mQuit.isEnabled()) {
            App.getInstance().getObservable().deleteObserver(this);
            super.onBackPressed();
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        if(arg instanceof Float) {
            float r = (float) arg;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String info = mInfo.get(0);
                    if(info.contains("%")) {
                        mInfo.remove(0);
                    }
                    mInfo.add(0,Util.getDateTime() + "  " + String.format("%.2f", r).toString() + "%");
                    mAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    public void download(View view) {
        long c = mPresenter.getUnUploadCount();
        if(c == 0) {
            mQuit.setEnabled(false);
            mUpload.setEnabled(false);
            mDownload.setEnabled(false);
            mPresenter.download();
        } else  {
            new AlertDialog.Builder(this).setMessage("有未上传数据，直接下载会丢弃数据，请确认是否继续")
                    .setPositiveButton("继续", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mQuit.setEnabled(false);
                            mUpload.setEnabled(false);
                            mDownload.setEnabled(false);
                            mPresenter.download();
                        }
                    }).setNegativeButton("取消",null).show();
        }
    }
}